package avicit.bdp.dms.prm.rest;

import avicit.bdp.common.dto.CommonVO;
import avicit.bdp.common.utils.enums.FileCategory;
import avicit.bdp.core.constant.Constants;
import avicit.bdp.dms.prm.dto.TagDTO;
import avicit.bdp.dms.prm.service.TagService;
import avicit.platform6.api.system.ConvertColumnClient;
import avicit.platform6.api.system.impl.SystemConstant;
import avicit.platform6.commons.utils.BusinessUtil;
import avicit.platform6.core.context.ThreadContextHelper;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 标签管理
 */
@RestController
@Api(tags = "标签管理REST接口")
@RequestMapping("/api/bdp/dms/prm/tag")
public class TagRest {

    @Autowired
    private TagService tagService;

    @Autowired
    private ConvertColumnClient convertColumnClient;

    /**
     * 新增标签对象
     */
    @PostMapping("/insertTag")
    @ApiOperation(value = "新增标签对象")
    public ResponseMsg<String> insertTag(@RequestBody TagDTO tag) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        tag.setOrgIdentity(ThreadContextHelper.getOrgId());
        responseMsg.setResponseBody(tagService.insertTag(tag));
        return responseMsg;
    }


    /**
     * 修改标签对象
     */
    @PostMapping("/updateTag")
    @ApiOperation(value = "修改标签对象")
    public ResponseMsg<String> updateTag(@RequestBody TagDTO tag) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        tagService.updateTag(tag);
        return responseMsg;
    }

    /**
     * 刪除数标签对象
     */
    @PostMapping("/deleteTag")
    @ApiOperation(value = "刪除数标签对象")
    public ResponseMsg<Integer> deleteTag(@RequestBody CommonVO commonVO) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        String id = commonVO.getId();
        tagService.deleteTag(id);
        return responseMsg;
    }

    /**
     * 修改标签状态
     */
    @PostMapping("/updateTagStatue")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "标签id集合", dataType = "String",  paramType = "body", required = true),
            @ApiImplicitParam(name = "tagStatus", value = "标签状态", dataType = "String", paramType = "body", required = true)
    })
    @ApiOperation(value = "修改标签状态")
    public ResponseMsg<String> updateTagStatue(@ApiIgnore @RequestBody Map queryMap) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String ids = (String) queryMap.get("ids");
        Integer status = (Integer) queryMap.get("tagStatus");
        tagService.updateTagStatue(ids, status);
        return responseMsg;
    }

    /**
     * 移动标签
     */
    @PostMapping("/moveTag")
    @ApiOperation(value = "移动标签")
    public ResponseMsg<String> moveTag(@RequestBody CommonVO commonVO) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String ids = commonVO.getIds();
        String parentId = commonVO.getParentId();
        tagService.moveTag(ids, parentId);
        return responseMsg;
    }

    /**
     * 根据分类ID查询标签
     */
    @GetMapping("/findTags")
    @ApiOperation(value = "标签列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类id", name = "categoryId", dataType = "String"),
            @ApiImplicitParam(value = "查询关键字", name = "keyWords", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    public ResponseMsg<QueryRespBean<TagDTO>> findTags(
            @RequestParam String categoryId,
            @RequestParam(required = false) String keyWords,
            @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(defaultValue = "10", required = false) Integer pageSize) {

        ResponseMsg<QueryRespBean<TagDTO>> responseMsg = new ResponseMsg<>();
        QueryRespBean<TagDTO> result = tagService.findTags(categoryId, keyWords, pageNo, pageSize);
        valueConvert(result.getResult());
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /**
     * 标签分类树查询
     */
    @GetMapping("/findTagCategory")
    @ApiOperation(value = "标签分类树查询")
    public ResponseMsg<List<TagDTO>> findTagCategory() {
        ResponseMsg<List<TagDTO>> responseMsg = new ResponseMsg<>();
        List<TagDTO> result = tagService.findTagCategory(FileCategory.CATEGORY.getCode(), null);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /**
     * 获取所有标签分类以及标签的树
     */
    @GetMapping("/findAllTags")
    @ApiOperation(value = "获取所有标签分类以及标签的树")
    public ResponseMsg<List<TagDTO>> findAllTags(@RequestParam(required = false) String secretLevel) {
        ResponseMsg<List<TagDTO>> responseMsg = new ResponseMsg<>();
        List<TagDTO> result = tagService.findTagCategory(FileCategory.FILE.getCode(), secretLevel);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /**
     * 判断标签分类是否重名
     */
    @GetMapping("/isSameCategoryName")
    @ApiOperation(value = "判断分类名称是否重复")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类id", name = "typeId", dataType = "String"),
            @ApiImplicitParam(value = "父节点ID", name = "parentId", dataType = "String"),
            @ApiImplicitParam(value = "分类名称", name = "name", dataType = "String")
    })
    public ResponseMsg<Boolean> isSameCategoryName(@RequestParam(required = false) String typeId,
                                                   @RequestParam String parentId,
                                                   @RequestParam String name
    ) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        Boolean flag = tagService.isSameCategoryName(typeId, parentId, name);
        responseMsg.setResponseBody(flag);
        return responseMsg;
    }

    @GetMapping("/isSameName")
    @ApiOperation(value = "判断英文名和编码是否重复")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "id", dataType = "String"),
            @ApiImplicitParam(value = "父节点ID", name = "parentId", dataType = "String"),
            @ApiImplicitParam(value = "分类名称", name = "name", dataType = "String")
    })
    public ResponseMsg<Boolean> isSameName(@RequestParam(required = false) String id,
                                           @RequestParam String value,
                                           @RequestParam String type
    ) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        Boolean flag = tagService.isSameName(id, value, type);
        responseMsg.setResponseBody(flag);
        return responseMsg;
    }

    /**
     * 通过平台API将字段值转换为名称，包括通用选择组件、通用代码
     */
    private void valueConvert(List<TagDTO> tagDTOList) {
        //循环组装请求数据
        Map<String, Set<String>> convertFormData = new HashMap<>();
        for (TagDTO tagDTO : tagDTOList) {
            BusinessUtil.createConvertSet(convertFormData, SystemConstant.USER, tagDTO.getCreatedBy());
            BusinessUtil.createConvertSet(convertFormData, Constants.BDP_DATA_SECRET_LEVEL_KEY, tagDTO.getSecretLevel());
        }
        if (convertFormData.size() > 0) {
            //获取请求结果
            Map<String, Map<String, String>> convertResultData = convertColumnClient.replace(convertFormData);
            //循环设置Alias或Name的值
            for (TagDTO tagDTO : tagDTOList) {
                tagDTO.setCreateUser(BusinessUtil.convertFormat(convertResultData, SystemConstant.USER,
                        tagDTO.getCreatedBy()));
                tagDTO.setSecretLevelName(BusinessUtil.convertFormat(
                        convertResultData, Constants.BDP_DATA_SECRET_LEVEL_KEY, tagDTO.getSecretLevel()));
            }
        }
    }
}
